//
//  HSChangeDataViewController.m
//  hsdyOrderSystem
//
//  Created by XunFengKJ on 2017/11/13.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HSChangeDataViewController.h"

@interface HSChangeDataViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;
//提交
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@end

@implementation HSChangeDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LCOLOROFBACK;
    self.inputTextField.text = self.defaultValue;
}

-(void)initViews{
    [self.submitBtn xw_roundedCornerWithRadius:8 cornerColor:lColorWhite];
    switch (self.comeType) {
        case 0:
            [self setTitle:@"店名"];
            break;
        case 1:
            [self setTitle:@"门店电话"];
            break;
        case 2:
            [self setTitle:@"门店地址"];
            break;
        case 3:
            [self setTitle:@"门店描述"];
            break;
        default:
            break;
    }
}

-(void)setTitle:(NSString *)tilte{
    self.navigationItem.title = tilte;
    self.inputTextField.placeholder = [NSString stringWithFormat:@"请输入%@",tilte];
}

//提交
- (IBAction)submitBtn:(id)sender {
    [self.inputTextField resignFirstResponder];
    NSString *str = self.inputTextField.text;
    switch (self.comeType) {
        case 0://店名
            [self changePostWithName:str address:nil phoneNumber:nil intro:nil];
            break;
        case 1://门店电话
            [self changePostWithName:nil address:nil phoneNumber:str intro:nil];
            break;
        case 2://门店地址
            [self changePostWithName:nil address:str phoneNumber:nil intro:nil];
            break;
        case 3://门店描述
            [self changePostWithName:nil address:nil phoneNumber:nil intro:str];
            break;
        default:
            break;
    }
}

-(void)changePostWithName:(NSString *)name address:(NSString *)address phoneNumber:(NSString *)phoneNumber intro:(NSString *)intro{
    //修改时间
    [self showLoadingWithClear:YES];
    WEAKSELF
    [XFPostRequest postChangeShopDataByShopId:[BookShareUserInforDefault readUserUid] shopImage:nil name:name address:address phoneNumber:phoneNumber begin:nil end:nil intro:intro success:^(id responseObject) {
        [weakSelf hideLoading];
        
        if ([responseObject[@"status"] intValue] == 200) {
            [MBProgressHUD showSuccess:@"修改成功"];
            [weakSelf save];
            [kNotificationCenter postNotificationName:changeShopDataNoti object:lIntToString(self.comeType) userInfo:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SOAutoHideMessageView showMessage:[responseObject safeJsonObjectForKey:@"msg"] inView:nil];
        }
    } failure:^(NSError *error){
        [weakSelf hideLoading];
        [MBProgressHUD showError:LNETWORKERROR];
    }];
}

-(void)save{
    switch (self.comeType) {
        case 0://店名
            [BookShareUserInforDefault saveUserNicknames:self.inputTextField.text];
             lNOTIF_POST(@"changeShopNameNoti", nil);
            break;
        case 1://门店电话
//            [NSKeyedArchiver archiveRootObject:self.inputTextField.text toFile:[NSString userDataPathWithPath:archiveShopPhoneData]];
            [BookShareUserInforDefault saveUserPhone:self.inputTextField.text];
            lNOTIF_POST(@"changeShopPhoneNoti", nil);
            break;
        case 2://门店地址
            [BookShareUserInforDefault saveUserAddress:self.inputTextField.text];
//            [NSKeyedArchiver archiveRootObject:self.inputTextField.text toFile:[NSString userDataPathWithPath:archiveShopAddressData]];
            break;
        case 3://门店描述
            [BookShareUserInforDefault saveUserIntro:self.inputTextField.text];
//            [NSKeyedArchiver archiveRootObject:self.inputTextField.text toFile:[NSString userDataPathWithPath:archiveShopintroData]];
            break;
        default:
            break;
    }
}

@end
